<template>
  <div v-if="show" class="fontDa" >
    <div v-for="hzItem in hzTableList" v-if="hzItem"  style="margin-bottom: 40px">
<!--    <div class="customTableHeader">-->
<!--      <div class="customTableTitle" @click="open(hzItem)">{{ hzItem.cepingHdglCppmxbInfo.title }}++</div>-->
<!--    </div>-->
      <div class="h_dw_time"  v-if="hzItem.cepingHdglCppmxbInfo">
        <div style="margin-left: 20px;font-size: 24px">单位：{{hzItem.cepingHdglCppmxbInfo.sheetDept}}</div>
      </div>
    <div class="fontDa">
      <div>
        <div class="biaoTiGi"  v-if="hzItem.cepingHdglCppmxbInfo">
          <span @click="open(hzItem)">{{ hzItem.cepingHdglCppmxbInfo.cppName }}</span>
        </div>
        <el-table :data="hzItem.tableData" border stripe style="width: 100%" :span-method="objectSpanMethod">
          <el-table-column  type="index"  label="序号" header-align="center" align="center" width="70"></el-table-column>
          <el-table-column v-for="(col,index) in hzItem.showColumns" :index="index" :prop="col.prop" :label="col.label"></el-table-column>
          <el-table-column
            label="操作"
            prop="caozuo"
            header-align="center"
            align="center"
            width="180">
            <template slot-scope="scope">
              <el-tooltip content="点击可返回该张测评票进行测评分值修改。" placement="top" effect="light" v-if="scope.$index==0">
                <el-button type="warning" style="margin-left:10px" @click="open(hzItem)">返回测评界面</el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    </div>
  </div>
</template>

<script>
import {wdXuanPiao, wdXuanPiaoXp} from "@/api/ceping/cepinghdglxpxxb";
import {anonymousSelhdidList as selhdidList} from '@/api/ceping/cepinghdglcppmxb'
import {wdXuanPiaoXpNew, xpInfoHz, xpInfo,wdXuanPiaoXpNewSm,xpInfoSm,xpInfoHzSm} from "@/api/ceping/jxevalvote";
import {getObj} from "@/api/ceping/cepinghdglb";
import {getIds as deptsGet} from "@/api/admin/dept";
import {getIds as usersGet} from "@/api/admin/user";

import djDf from "./cpIndexHzDjdf";

export default {
  name: "cpIndexHz",
  components: {
    djDf
  },
  data() {
    return {
      show: true,
      type: "",
      hdId: null,
      xcCode: "",
      xpList: [],//所有选票
      cppList: [],
      cepingHdglb: {},
      deptObj: {},
      userObj: {},
      djxxDjmx: false,
      tableData: [],
      cepingHdglCppmxbInfo: {},
      colShowAll: [],
      hzTableList: [],
      tabsList:[],
      fromPage:'',
      fromXpId:'',
      fromcppId:''
    }
  },
  methods: {
    init() {
      this.xpList = [];
    },
    //实名打票过来的汇总初始化
    InitHdHz(data){
      this.xcCode = data.xpCode;
      this.tabsList=data.tabsList;
      this.fromPage=data.fromPage;
      this.fromXpId=data.fromXpId;
      this.fromcppId=data.fromcppId;
      this.hzTableList = [];
      this.hdId=data.hdId;
      this.userId=this.$store.state.user.userInfo.userId;
      const params={
        hdId:this.hdId,
        userId:this.userId,
        sheetType:4
      }
      wdXuanPiaoXpNewSm(params).then(res => {
        this.xpList = res.data.data;
        this.hzTableList = res.data.data;
        this.xpList.forEach((item, index) => {
          //获取表头
          this.getHdColum(item.cppId, index);
          //获取表格数据
          //this.getHzData(item.cppId, index);
        })
        this.$forceUpdate();
      })
    },
    //实名打票//获取汇总表格表头
    getHdColum(cppId, index) {
      var dataList = [];
      let params ={
        cppId:cppId,
        hdId:this.hdId,
        userId:this.userId
      }
      xpInfoSm(params).then(res => {
        this.hzTableList[index].cepingHdglCppmxbInfo = res.data.data.cepingHdglCppmxbInfo;
        dataList = JSON.parse(res.data.data.cepingHdglCppmxbInfo.hostConfigJson);
        this.hzTableList[index].showColumns = dataList;
        this.$forceUpdate()
        this.getHdHzData(cppId, index)
      })
      // return dataList;
    },
    getHdHzData(cppId,index){
      var dataList = [];
      let params ={
        cppId:cppId,
        sheetType:4,
        hdId:this.hdId,
        userId:this.userId
      }
      xpInfoHzSm(params).then(res => {
        res.data.data.userList.forEach(item=>{
                if(item.rySr){
                  item.rySr = item.rySr.substring(0,7)
                }
              })
        dataList = res.data.data.userList;
        this.hzTableList[index].tableData = dataList;
        this.$emit('closeLoading')
        this.$forceUpdate()
      })
    },
    //汇总初始化
    initHz(data) {
      this.xcCode = data.xpCode;
      this.tabsList=data.tabsList;
      this.fromPage=data.fromPage;
      this.fromXpId=data.fromXpId;
      this.fromcppId=data.fromcppId;
      this.hzTableList = [];
      var params = {
        xpCode: this.xcCode,
        sheetType:4
      }
      wdXuanPiaoXpNew(params).then(res => {
        this.xpList = res.data.data;
        this.hzTableList = res.data.data;
        this.xpList.forEach((item, index) => {
          //获取表头
          this.getColum(item.cppId, index);
          //获取表格数据
          //this.getHzData(item.cppId, index);
        })
        this.$forceUpdate();
      })
    },
    //初始按选票
    initXp(xcCode) {
      this.init();
      this.type = "XP";
      this.xcCode = xcCode;
      wdXuanPiaoXp(xcCode).then(res => {
        if (res.data.data.length > 0) {
          let data = res.data.data;
          this.initJG(data);
        } else {
          this.$notify.closeAll();
          this.$notify.error('票号中没有选票信息');
        }
      })
    },
    initJG(d) {
      let data = d.sort(compareZb);

      function compareZb(a, b) {
        return b.cphzfz - a.cphzfz;
      }

      this.cppList = [];
      let cppListId = [];

      selhdidList(data[0].hdId).then(res => {
        let list = res.data.data;
        this.djxxDjmx = false;
        for (var j in list) {
          if (!(list[j].djxxDjmx == "" || list[j].djxxDjmx == null)) {
            this.djxxDjmx = true;
          }
          if (cppListId.indexOf(list[j].cppId) == -1) {
            for (let i in data) {
              if (data[i].cppId == list[j].cppId) {
                this.cppList.push(list[j])
                cppListId.push(list[j].cppId)
                break;
              }
            }
          }
        }
        for (let i in data) {
          if (data[i].bcpDx == "部门") {
            this.xpList.push(data[i])
          }
        }
        for (let j in data) {
          if (data[j].bcpDx == "个人") {
            this.xpList.push(data[j])
          }
        }
        this.getHdInfo();
        this.getDeptUser();
      })
    },
    getXpList(cp) {
      let list = [];
      for (var i in this.xpList) {
        if (this.xpList[i].cppId == cp.cppId) {
          list.push(this.xpList[i])
        }
      }
      return list;
    },
    getHdInfo() {
      if (this.xpList.length > 0) {
        getObj(this.xpList[0].hdId).then(res => {
          this.cepingHdglb = res.data.data;
        })
      }
    },
    getDeptUser() {
      let deptIds = [];
      let userIds = [];
      for (var i in this.xpList) {
        let r = this.xpList[i];
        if (r.bcpDx == "部门") {
          deptIds.push(r.bcpDeptId)
        }
        if (r.bcpDx == "个人") {
          userIds.push(r.bcpUserId)
        }
      }
      if (deptIds.length > 0) {
        deptsGet(deptIds.join()).then(res => {
          for (var i in res.data.data) {
            this.deptObj[res.data.data[i].deptId] = res.data.data[i];
            for (var j in this.xpList) {
              let r = this.xpList[j];
              if (r.bcpDx == "部门") {
                if (res.data.data[i].deptId == r.bcpDeptId) {
                  this.$set(r, "deptInfo", res.data.data[i]);
                }
              }
            }
          }
        })
      }
      if (userIds.length > 0) {
        usersGet(userIds.join()).then(res => {
          for (var i in res.data.data) {
            this.userObj[res.data.data[i].userId] = res.data.data[i];
            for (var j in this.xpList) {
              let r = this.xpList[j];
              if (r.bcpDx == "个人") {
                if (res.data.data[i].userId == r.bcpUserId) {
                  this.$set(r, "userInfo", res.data.data[i]);
                }
              }
            }
          }

        })
      }
    },
    open(t) {
      var params={
        id:t.cppId,
        clickTab:'ceping4',
        upPageObj:{
          toPage:this.fromPage,
          tpXpId:this.fromXpId,
          cppId:this.fromcppId
        }
      }
      this.$emit('openXp', params);
    },
    //获取汇总表格数据
    getHzData(cppId, index) {
      var dataList = [];
      var params={
        xpCode:this.xcCode,
        sheetType:4,
        cppId:cppId
      }
      xpInfoHz(params).then(res => {
        res.data.data.userList.forEach(item=>{
                if(item.rySr){
                  item.rySr = item.rySr.substring(0,7)
                }
              })
        dataList = res.data.data.userList;
        this.hzTableList[index].tableData = dataList;
        this.$emit('closeLoading')
        this.$forceUpdate()
      })
    },
    //获取汇总表格表头
    getColum(cppId, index) {
      var dataList = [];
      var params={
        cppId:cppId,
        xpCode:this.xcCode
      }
      xpInfo(params).then(res => {
        this.hzTableList[index].cepingHdglCppmxbInfo = res.data.data.cepingHdglCppmxbInfo;
        dataList = JSON.parse(res.data.data.cepingHdglCppmxbInfo.hostConfigJson);
        this.hzTableList[index].showColumns = dataList;
        this.$forceUpdate()
        this.getHzData(cppId, index)
      })
      // return dataList;
    },
    //合并单元格
    objectSpanMethod({row, column, rowIndex, columnIndex}) {
      if(column.property=="caozuo"){
        return [99999, 1];
      }
    },
  }
}
</script>

<style scoped>
.customTableTitle:hover{
  color: #1a71fb;
  cursor: pointer;
}
.biaoTiGi {
  width: 100%;
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 28px;
  font-weight: bolder;
  /*background: #1f84ec;*/
  background: #b0bde2;
  /*color: #fff;*/
}
.biaoTiGi span:hover{
  color: #1a71fb;
  cursor: pointer;
}
</style>
